<?php
class Admin_setting extends MY_Controller {
	public function __construct()
	{
		$this->data["menu"] = "setting";
		parent::__construct();		
		$this->data["base_link"] = base_index_admin . "setting/";
	}
	
	public function index()
	{	
		if(empty($_SESSION["code"])){			
			$setting = $this->settingModel->get_setting($this->data["arr_lang"]);
			extract($setting);
			$table = "site_settingLan";
			$where = "code = '$code'";	
			lang_addData($table, $where, $this->data["arr_fields_setting"], $this->data["arr_lang"]);			
		}
		else{
			extract($_SESSION);
		}
		
		$err_chk = "";
		$errormsg_shopTel = "";
		$errormsg_shopEmail = "";
		$errormsg_shopEmailPass = "";
		$errormsg_paging_value = "";
				
		if(!empty($_SESSION["err"])){
			$err = $_SESSION["err"];		
			if(preg_match("/01/", $err)){
				$errormsg_shopTel = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errormsg_shopTel"]."</span>";
				$err_chk++;
			}
			if(preg_match("/02/", $err)){
				$errormsg_shopEmail = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errormsg_shopEmail_format"]."</span>";
				$err_chk++;
			}			
			if(preg_match("/03/", $err)){
				$errormsg_paging_value = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errormsg_paging_value"]."</span>";
				$err_chk++;
			}
			if(preg_match("/04/", $err)){
				$errormsg_paging_value = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errormsg_paging_value_int"]."</span>";
				$err_chk++;
			}			
		}
		unset($_SESSION["err"]);		
		$this->data["errormsg_shopTel"] = $errormsg_shopTel;
		$this->data["errormsg_shopEmail"] = $errormsg_shopEmail;
		$this->data["errormsg_shopEmailPass"] = $errormsg_shopEmailPass;
		$this->data["errormsg_paging_value"] = $errormsg_paging_value;
				
		$lang_fields = lang_createFields($this->data["arr_lang"], $this->data["arr_fields_setting"], $this->data["arr_lang_chk_empty"], $this->data["lang"]["general"]["err_empty"]);
		
		$this->data["code"] = $code;
		$this->data["shopTel"] = $shopTel;
		$this->data["shopFax"] = $shopFax;
		$this->data["shopEmail"] = $shopEmail;
		$this->data["shopEmailPass"] = $shopEmailPass;
		$this->data["shopYahoo"] = $shopYahoo;
		$this->data["shopSkype"] = $shopSkype;
		$this->data["shopFacebook"] = $shopFacebook;
		$this->data["shopTwitter"] = $shopTwitter;
		$this->data["shopGoogle"] = $shopGoogle;
		$this->data["paging_value"] = $paging_value;
		$this->data["lang_fields"] = $lang_fields;
		@$this->data["msg"] = $_SESSION["msg"];

		unset($_SESSION["code"]);
		unset($_SESSION["shopTel"]);
		unset($_SESSION["shopFax"]);
		unset($_SESSION["shopEmail"]);
		unset($_SESSION["shopEmailPass"]);
		unset($_SESSION["paging_value"]);
		unset($_SESSION["shopYahoo"]);
		unset($_SESSION["shopSkype"]);
		unset($_SESSION["shopFacebook"]);
		unset($_SESSION["shopTwitter"]);
		unset($_SESSION["shopGoogle"]);
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/setting/setting', $this->data);
	}
	
	public function done()
	{
		if(empty($_POST)){
			header("Location: " . $this->data["base_link"]);
			exit();
		}
			
		POST_To_SESSION();
		
		lang_checkErrEmpty($this->data["arr_lang"], $this->data["arr_fields_setting"], $this->data["arr_lang_chk_empty"]);

		extract($_POST);
		
		$err = "";
		if(empty($shopTel)){
			$err .= "+01";
		}		
		if(!empty($shopEmail)){
			$regex = "/^([a-z0-9_\.-])+@(([a-z0-9_-])+\\.)+[a-z]{2,4}$/";		
			if(!preg_match($regex, $shopEmail)){
				$err .= "+02";
			}
		}
		if(empty($paging_value))
			$err .= "+03";
		elseif(is_numeric($paging_value) === false){
			$err .= "+04";
		}

		$_SESSION["err"] = $err;		
		if(!empty($_SESSION["err_lang"]) || !empty($err)){
			header("Location: " . $this->data["base_link"]);
			exit();
		}
		
		
		$data = array();
		$data["shopTel"] = $shopTel;		
		$data["shopFax"] = $shopFax;
		$data["shopEmail"] = $shopEmail;
		$data["shopEmailPass"] = $shopEmailPass;
		$data["paging_value"] = $paging_value;
		$data["shopYahoo"] = $shopYahoo;
		$data["shopSkype"] = $shopSkype;
		$data["shopFacebook"] = $shopFacebook;
		$data["shopTwitter"] = $shopTwitter;
		$data["shopGoogle"] = $shopGoogle;

		$this->settingModel->update_entry($code, $data);		
		$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["upload_done"] . "</p>";
		
		$def = array("code" => $code);	
		$table = "site_settingLan";
		$colID = "site_settingLan_id";
		lang_accesstData($table ,$this->data["arr_lang"], $this->data["arr_fields_setting"], $def, $this->data["arr_lang_chk_empty"], $colID);
			
		//unset
		unsetPOSTSESSION();
		unset($_SESSION["code"]);
		unset($_SESSION["shopTel"]);
		unset($_SESSION["shopFax"]);
		unset($_SESSION["shopEmail"]);
		unset($_SESSION["shopEmailPass"]);
		unset($_SESSION["paging_value"]);
		unset($_SESSION["shopYahoo"]);
		unset($_SESSION["shopSkype"]);
		unset($_SESSION["shopFacebook"]);
		unset($_SESSION["shopTwitter"]);
		unset($_SESSION["shopGoogle"]);
		unset($_SESSION["err"]);	
		
		header("Location: " . $this->data["base_link"]);
	}	
	
	public function changepass()
	{		
		$err_chk = "";
		$errorms_password_old = "";
		$errorms_password_new = "";
		$errorms_password_confirm = "";
		if(!empty($_SESSION["err"])){
			$err = $_SESSION["err"];		
			if(preg_match("/01/", $err)){
				$errorms_password_old = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errorms_password_old"]."</span>";
				$err_chk++;
			}			
			if(preg_match("/02/", $err)){
				$errorms_password_old = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errorms_password_old_2"]."</span>";
				$err_chk++;
			}
			if(preg_match("/03/", $err)){
				$errorms_password_new = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errorms_password_new"]."</span>";
				$err_chk++;
			}
			if(preg_match("/04/", $err)){
				$errorms_password_confirm = "<br /><span class=\"required\">".$this->data["lang"]["setting"]["errorms_password_confirm"]."</span>";
				$err_chk++;
			}			
		}
		unset($_SESSION["err"]);
		$this->data["errorms_password_old"] = $errorms_password_old;
		$this->data["errorms_password_new"] = $errorms_password_new;
		$this->data["errorms_password_confirm"] = $errorms_password_confirm;
		@$this->data["msg"] = $_SESSION["msg"];
		unset($_SESSION["msg"]);
		$this->load->view('admin/setting/setting_changepass', $this->data);		
	}
	
	public function changepass_done()
	{
		if(empty($_POST)){
			header("Location: " . $this->data["base_link"] . "changepass/");
			exit();
		}
		
		@$password_old = $_POST["password_old"];
		@$password_new = $_POST["password_new"];		
		@$password_confirm = $_POST["password_confirm"];		
		
		$err = "";
		if(empty($password_old)){
			$err .= "+01";
		}
		else{
			$query = $this->admin_userModel->get_where(array("userID" => $_SESSION["my_admin_id"], "userPass" => $password_old));
			if($query->num_rows < 1)
				$err .= "+02";
		}
		if(empty($password_new)){
			$err .= "+03";
		}
		elseif($password_new != $password_confirm){
			$err .= "+04";
		}
				
		$_SESSION["err"] = $err;		
		if(!empty($err)){
			header("Location: " . $this->data["base_link"] . "changepass/");
			exit();
		}
		
		$data = array();
		$data["userPass"] = $password_new;
		$this->admin_userModel->update_entry($_SESSION["my_admin_id"], $data);
		$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["upload_done"] . "</p>";
		
		//unset
		unset($_SESSION["err"]);		
		
		header("Location: " . $this->data["base_link"] . "changepass/");
	}
	
	public function intro()
	{
		$this->data["type"] = "intro";
		$this->general();
	}
	
	public function general()
	{	
		$site_general = $this->settingModel->get_site_general($this->data["type"], $this->data["arr_lang"]);
		extract($site_general);
		$table = "site_generalLan";
		$where = "code = '$code'";
		$this->data["arr_fields_site_general"] = array("content" => array("title" => $this->data["lang"]["general"]["content"], "required" => false, "type" => "ckeditor"));
		lang_addData($table, $where, $this->data["arr_fields_site_general"], $this->data["arr_lang"]);
		
		$lang_fields = lang_createFields($this->data["arr_lang"], $this->data["arr_fields_site_general"], array(), $this->data["lang"]["general"]["err_empty"]);
		
		$this->data["code"] = $code;		
		$this->data["lang_fields"] = $lang_fields;
		
		@$this->data["msg"] = $_SESSION["msg"];		
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/setting/setting_general', $this->data);
	}
	
	
	
	public function general_done()
	{
		if(empty($_POST)){
			header("Location: " . $this->data["base_link"]);
			exit();
		}
		
		$type = $this->settingModel->update_site_general($_POST["code"], $this->data["arr_lang"], $_POST);
		$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["upload_done"] . "</p>";
		
		header("Location: " . $this->data["base_link"] . $type);
	}
}